﻿namespace ChangchunPulbicServicesImp;

public class PoiQueryServices : IPoiQuery
{
    private IHttpClientFactory HttpClientFactory { get; init; }

    private IConfigServices ConfigServices { get; init; }

    public PoiQueryServices(IHttpClientFactory httpClientFactory, IConfigServices configServices)
    {
        HttpClientFactory = httpClientFactory;
        ConfigServices = configServices;
    }

    public async Task<PoiQueryResult> CircleQueryAsync(string keyword, int page, Point center, double radius, string? tag = null)
    {
        ///验证参数
        if (!ValidationHelper.IsKeywordLegal(keyword))
            return new PoiQueryResult()
            {
                StatusCode = Code.WrongValidation,
                Message = "关键词参数格式不符合要求",
            };
        if(radius<=0)
            return new PoiQueryResult()
            {
                StatusCode = Code.WrongValidation,
                Message = "半径参数不符合要求",
            };

        ///请求API
        var client = HttpClientFactory.CreateClient();
        var apiResult = await APIHelper.GetAsync<Place>(client,
            $"https://api.map.baidu.com/place/v2/search?query={keyword}&tag={tag}&page_num={page}&location={center.ToReverseCoordinate()}&radius={radius}&output=json&ak={ConfigServices.AK}");

        ///查询失败
        if (!apiResult.Succeed)
            return new PoiQueryResult()
            {
                StatusCode = apiResult.StatusCode,
                Message = "查询失败",
                QueryResultFrom3rdPartyApi = apiResult,
            };

        ///验证结果
        var resultObj = apiResult.ResultObject;
        ///解析错误
        if (resultObj is null)
            return new PoiQueryResult()
            {
                StatusCode = Code.InternalFault,
                Message = "解析查询结果时，程序发生内部错误",
                QueryResultFrom3rdPartyApi = apiResult,
            };
        if (resultObj.results is null)
            return new PoiQueryResult()
            {
                StatusCode = Code.InternalFault,
                Message = "解析查询结果时，程序发生内部错误",
                QueryResultFrom3rdPartyApi = apiResult,
            };
        if (resultObj.results.Count is 0)
            return new PoiQueryResult()
            {
                StatusCode = Code.NoQueryResult,
                Message = "查询成功，但附近没有可用的查询结果",
                QueryResultFrom3rdPartyApi = apiResult,
            };

        return new PoiQueryResult()
        {
            StatusCode = Code.Succeed,
            Message = "查询成功",
            QueryResultFrom3rdPartyApi = apiResult,
        };
    }

    public async Task<PoiQueryResult> KeywordQueryAsync(string keyword, int page, string? tag = null)
    {
        ///验证参数
        if (!ValidationHelper.IsKeywordLegal(keyword))
            return new PoiQueryResult()
            {
                StatusCode = Code.WrongValidation,
                Message = "关键词参数格式不符合要求",
            };

        ///请求API
        var client = HttpClientFactory.CreateClient();
        var apiResult = await APIHelper.GetAsync<Place>(client,
            $"https://api.map.baidu.com/place/v2/search?query={keyword}&tag={tag}&region={ConfigServices.CityName}&page_num={page}&output=json&ak={ConfigServices.AK}");

        ///查询失败
        if (!apiResult.Succeed)
            return new PoiQueryResult()
            {
                StatusCode = apiResult.StatusCode,
                Message = "查询失败",
                QueryResultFrom3rdPartyApi = apiResult,
            };

        ///验证结果
        var resultObj = apiResult.ResultObject;
        ///解析错误
        if (resultObj is null)
            return new PoiQueryResult()
            {
                StatusCode = Code.InternalFault,
                Message = "解析查询结果时，程序发生内部错误",
                QueryResultFrom3rdPartyApi = apiResult,
            };
        if (resultObj.results is null)
            return new PoiQueryResult()
            {
                StatusCode = Code.InternalFault,
                Message = "解析查询结果时，程序发生内部错误",
                QueryResultFrom3rdPartyApi = apiResult,
            };
        if (resultObj.results.Count is 0)
            return new PoiQueryResult()
            {
                StatusCode = Code.NoQueryResult,
                Message = "查询成功，但没有可用的结果",
                QueryResultFrom3rdPartyApi = apiResult,
            };

        return new PoiQueryResult()
        {
            StatusCode = Code.Succeed,
            Message = "查询成功",
            QueryResultFrom3rdPartyApi = apiResult,
        };
    }

    public async Task<LocationDecodeResult> LocationDecodeQueryAsync(Location location)
    {
        ///请求API
        var client = HttpClientFactory.CreateClient();
        var apiResult = await APIHelper.GetAsync<DeCodeInfo>(client,
            $"https://api.map.baidu.com/reverse_geocoding/v3/?ak={ConfigServices.AK}&output=json&location={location.ToPoint().ToReverseCoordinate()}");

        var result= new LocationDecodeResult()
        {
            StatusCode = apiResult.StatusCode,
            Message = "查询失败",
            QueryResultFrom3rdPartyApi = apiResult,
        };

        ///查询失败
        if (!apiResult.Succeed)
            return result;

        ///验证结果
        if(result.Address is null)
        {
            result.Message = "查询成功，但是解析结果为空";
            return result;
        }

        result.Message = "查询成功";
        return result;
    }
}
